﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json;

namespace ExcelJsonParser
{
    class JsonWorkBook : JsonWorkBookBase
    {
        public JsonWorkBook(Workbook wb)
            : base(wb)
        {
            _jsonWorkSheets = new JsonWorkSheets(wb.Worksheets);
        }

        protected override void Init(Workbook wb)
        {
        }

        internal void ImportJson(object javaObject)
        {
            //if (javaObject is JavaScriptObject)
            //{
            //    JavaScriptObject jsonObject = javaObject as JavaScriptObject;
            //    Boolean isAllDeepJsonObject = true;
            //    foreach (string key in jsonObject.Keys)
            //    {
            //        if (jsonObject[key] is JavaScriptArray || jsonObject[key] is JavaScriptObject)
            //        {
            //            isAllDeepJsonObject = true;
            //        }
            //        else
            //        {
            //            isAllDeepJsonObject = false;
            //            break;
            //        }
            //    }
            //    if (isAllDeepJsonObject)
            //    {
            //        foreach (string key in jsonObject.Keys)
            //        {
            //            JsonWorkSheet jsonWs = null;
            //            if (_jsonWorkSheets.TryGetWorkSheetByName(key, out jsonWs) == false)
            //            {
            //                Worksheet ws = _workbook.Worksheets.Add();
            //                ws.Name = key;
            //                //jsonWs = new JsonWorkSheet(ws);
            //                jsonWs = _jsonWorkSheets.Add(ws);
            //            }
            //            jsonWs.SetJsonSource(jsonObject[key]);
            //        }
            //        return;
            //    }
            //}
            //if (javaObject is JavaScriptArray || javaObject is JavaScriptObject)
            //{
            //    JsonWorkSheet jsonWs = null;
            //    if (_jsonWorkSheets.TryGetWorkSheetByName(_workbook.ActiveSheet.Name, out jsonWs))
            //    {
            //        jsonWs.SetJsonSource(javaObject);
            //    }
                
            //}
            if (javaObject is JavaScriptObject)
            {
                JavaScriptObject jsonObject = javaObject as JavaScriptObject;
                foreach (string key in jsonObject.Keys)
                {
                    JsonWorkSheet jsonWs = null;
                    if (_jsonWorkSheets.TryGetWorkSheetByName(key, out jsonWs) == false)
                    {
                        Worksheet ws = _workbook.Worksheets.Add();
                        ws.Name = key;
                        //jsonWs = new JsonWorkSheet(ws);
                        jsonWs = _jsonWorkSheets.Add(ws);
                    }
                    jsonWs.SetJsonSource(jsonObject[key]);
                }
            }
            if (javaObject is JavaScriptArray)
            {
                JsonWorkSheet jsonWs = null;
                if (_jsonWorkSheets.TryGetWorkSheetByName(_workbook.ActiveSheet.Name, out jsonWs))
                {
                    jsonWs.SetJsonSource(javaObject);
                }
            }
        }

        internal object ExportCurrentWorkSheet()
        {
            JsonWorkSheet jsonWs = null;
            object javaObject = null;
            if (_jsonWorkSheets.TryGetWorkSheetByName(_workbook.ActiveSheet.Name, out jsonWs))
            {
                javaObject = jsonWs.ExportJson();
                
            }
            return javaObject;
        }

        internal object ExportCurrentWorkBook()
        {
            JavaScriptObject jsonObj = new JavaScriptObject();
            foreach (Worksheet ws in _workbook.Sheets)
            {
                JsonWorkSheet jsonWs = null;
                object javaObject = JsonWorkCell.jsonUndefined;
                if (_jsonWorkSheets.TryGetWorkSheetByName(ws.Name, out jsonWs))
                {
                    javaObject = jsonWs.ExportJson();
                }
                if (javaObject is JsonUndefined)
                {
                    
                }
                else
                {
                    jsonObj.Add(ws.Name, javaObject);
                }
            }
            return jsonObj;
        }

        internal void ImportJson2CurrentWorkSheet(object javaObject)
        {
            JsonWorkSheet jsonWs = null;
            if (_jsonWorkSheets.TryGetWorkSheetByName(_workbook.ActiveSheet.Name, out jsonWs))
            {
                jsonWs.SetJsonSource(javaObject);
            }
        }
    }
}
